package calc

import (
	"fmt"
	"gitee.com/golang_example/calcproj/src/simplemath"
	"os"
	"strconv"
)

var Usage = func(){
	fmt.Printf("USAGE:calc command [arguments] ...")
	fmt.Printf("\nThe commands are:\n\tadd\tAddition of two values.\n\tsqrt\tSquare root of a non-negative value.\n\t")
}

func calc(){
	args := os.Args[1:]
	if args == nil || len(args) < 2{
		Usage()
		return
	}
	
	switch args[0] {
	case "add":
		if len(args) != 3 {
			fmt.Printf("USAGE: calc add <integer1><integer2>")
			return
		}
		v1, err1 := strconv.Atoi(args[1])
		v2, err2 := strconv.Atoi(args[2])
		if err1 != nil || err2 != nil {
			fmt.Printf("USAGE:calc add <integer1><integer2>")
		}
		ret := simplemath.Add(v1, v2)
		fmt.Print("Result: ", ret)
		fmt.Print("\n")
	case "sqrt":
		if len(args) != 2{
			fmt.Print("USAGE: calc sqrt <integer>")
			return
		}
		v, err := strconv.Atoi(args[1])
		if err != nil {
			fmt.Print("USAGE: calc sqrt <integer>")
			fmt.Print("\n")
			return
		}
		ret := simplemath.Sqrt(v)
		fmt.Print("Result: ", ret)
		fmt.Print("\n")
	default:
		Usage()
		fmt.Print("\n")
	}
}